﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Parser;

namespace Service
{
    /// <summary>
    /// Represents the api of the statistics service platform
    /// </summary>
    public class StatisticsService : IStatisticsService
    {
        /// <summary>
        /// Database model
        /// </summary>
        DatabaseModelTableAdapters.TableAdapterManager tableAdapter;

        public void Init() {
            tableAdapter = new Service.DatabaseModelTableAdapters.TableAdapterManager();
            tableAdapter.LogFilesTableAdapter = new Service.DatabaseModelTableAdapters.LogFilesTableAdapter();
            tableAdapter.PagesTableAdapter = new Service.DatabaseModelTableAdapters.PagesTableAdapter();
            tableAdapter.SitesTableAdapter = new Service.DatabaseModelTableAdapters.SitesTableAdapter();
            tableAdapter.VisitsTableAdapter = new Service.DatabaseModelTableAdapters.VisitsTableAdapter();
        }

        #region IStatisticsService Member

        public void AddVisitor(int siteId,
            string visitorIp,
            DateTime firstAction,
            DateTime lastAction,
            int totalAction,
            long siteEntryId,
            string referer,
            string os,
            string webbrowser,
            string webbrowserVersion,
            long trafficSent,
            long trafficGet,
            int siteVisits,
            int visitorType)
        {
            tableAdapter.VisitsTableAdapter.Insert(
                siteId,
                visitorIp,
                firstAction,
                lastAction,
                siteEntryId,
                totalAction,
                referer,
                os,
                webbrowser,
                webbrowserVersion,
                "",
                trafficSent,
                trafficGet,
                siteVisits,
                visitorType);
        }

        public void AddPage(int siteId, string url)
        {
            if (tableAdapter.PagesTableAdapter.ExistsUrl(url, siteId) == null)
            {
                tableAdapter.PagesTableAdapter.Insert(siteId, url);
            }
        }

        public long GetPage(int siteId, string url)
        {
            return (long)tableAdapter.PagesTableAdapter.GetSiteId(url, siteId);
        }

        public Shared.Site GetSite(int siteId)
        {
            Shared.Site returnValue = new Shared.Site();
            DatabaseModel.SitesRow result = tableAdapter.SitesTableAdapter.GetDataById(siteId)[0];

            returnValue.Id = result.SiteId;
            returnValue.Name = result.Name;
            returnValue.InternalName = result.SiteName;
            returnValue.LogPath = result.LogPath;

            return returnValue;
        }

        public Shared.Logfile GetLogFile(int siteId, string logFile)
        {
            Shared.Logfile returnValue = new Shared.Logfile();
            DatabaseModel.LogFilesRow result;
            try
            {
                result = tableAdapter.LogFilesTableAdapter.GetDataByFile(logFile, siteId)[0];
            }
            catch
            {
                return null;
            }

            returnValue.Id = result.Id;
            returnValue.SiteId = result.SiteId;
            returnValue.LogFile = result.LogFile;
            returnValue.Processed = (result.Processed == 1);

            return returnValue;
        }

        public void  AddLogFile(Shared.Logfile logfile)
        {
            tableAdapter.LogFilesTableAdapter.Insert(logfile.SiteId, logfile.LogFile, logfile.CurrentLine, (logfile.Processed) ? 1 : 0);
        }

        public void  UpdateLogFile(Shared.Logfile logfile)
        {
            tableAdapter.LogFilesTableAdapter.Update(logfile.SiteId, logfile.LogFile, logfile.CurrentLine, logfile.Processed ? 1 : 0, logfile.Id);
        }

#endregion
}
}
